########################################
#### figure_1.R: Generates Figure 1 ####
########################################

df_sub <- dta_midterm

df_sub$w9_2022_1_elections1_raw = df_sub$w9_2022_1_elections1
df_sub$w9_2022_1_elections1 <- recode(df_sub$w9_2022_1_elections1, '1' = "Agree", "2" = "Agree", "3" = "Don't Know", "4" = "Disagree", "5" = "Disagree")
df_sub$w9_2022_1_elections3 <- recode(df_sub$w9_2022_1_elections3, '1' = 'Yes', '2' = 'No', '3' = "Don't know")

acc_res_ful <- pollster::moe_topline(df_sub, variable=w9_2022_1_elections1, weight=weight) %>%
  mutate(upper = Percent+MOE,
         lower = Percent-MOE,
         wn = round(Frequency*(Percent/100),0),
         q = "Do you agree or disagree that US elections\nproduce accurate results?",
         pid = "Pooled") |> 
  select(q, pid, Response, Percent, lower, upper, wn)

acc_res_pid = pollster::moe_crosstab(df_sub, y=w9_2022_1_elections1, x=pid, weight=weight) %>%
  mutate(upper = pct+moe,
         lower = pct-moe,
         wn = round(n*(pct/100),0),
         Percent = pct,
         Response = w9_2022_1_elections1,
         q = "Do you agree or disagree that US elections\nproduce accurate results?") |> 
  select(q, pid, Response, Percent, lower, upper, wn)

frd_res_ful = pollster::moe_topline(df_sub, variable=w9_2022_1_elections3, weight=weight) %>%
  mutate(upper = Percent+MOE,
         lower = Percent-MOE,
         wn = round(Frequency*(Percent/100),0),
         q = "Do you think voter fraud occurred\nin the 2022 midterms?",
         pid = "Pooled") |> 
  select(q, pid, Response, Percent, lower, upper, wn)

frd_res_pid = pollster::moe_crosstab(df_sub, y=w9_2022_1_elections3, x=pid, weight=weight) %>%
  mutate(upper = pct+moe,
         lower = pct-moe,
         wn = round(n*(pct/100),0),
         Percent = pct,
         Response = w9_2022_1_elections3,
         q = "Do you think voter fraud occurred\nin the 2022 midterms?") |> 
  select(q, pid, Response, Percent, lower, upper, wn)

results = bind_rows(acc_res_ful, acc_res_pid, frd_res_ful, frd_res_pid) |> 
  mutate(
    Response = case_when(
      Response %in% c("Yes", "Agree") ~ "Agree (Yes)",
      Response %in% c("No", "Disagree") ~ "Disagree (No)",
      .default = "Don't Know"),
    pid = factor(pid, levels = c("Pooled","Republican", "Democrat", "Independent"))
  )

fig1 = ggplot(results, aes(x = Percent, y = Response)) +
  geom_pointrange(aes(xmin = lower, xmax = upper, color = Response),
                  show.legend = F) +
  geom_text(aes(x = upper + 8,label = paste(round(Percent,1),"%",sep="")), 
            fontface = "bold", vjust = .5,
            position = position_dodge(1), size = 4) +
  scale_color_manual(values = colors[1:3]) +
  scale_x_continuous(limits = c(-10,100), name = "Percent",
                     breaks = seq(0,100,25), labels = paste0(seq(0,100,25),"%")) +
  facet_grid(cols = vars(q), rows = vars(pid),
             scales = "free_y", drop = T) +
  theme_prl() + 
  theme(panel.grid.major = element_blank(),
        plot.title = element_text(size=12),
        legend.text=element_text(size=9),          
        axis.title.y=element_blank(),
        axis.title.x=element_blank(), 
        legend.position = "none") +
  labs(caption = "Weighted Percentages and 95% CIs")

ggsave(here("Figures", "figure_1.pdf"), fig1,
       width=8, height=5, units = 'in', dpi = 600)
